//
//  KKChatCell.m
//  DoMi
//
//  Created by 康凯 on 2016/12/7.
//  Copyright © 2016年 kk. All rights reserved.
//
#define voiceWidth 300
#define voiceHeight 30
#import "KKChatCell.h"
#import "KKAdviceDetailM.h"

@implementation KKChatCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backImg = [[UIImageView alloc] init];
        [self.contentView addSubview:self.backImg];
        
        self.content = [[UILabel alloc] init];
        self.content.numberOfLines = 0;
        self.content.font = [UIFont systemFontOfSize:17.0f];
        [self.backImg addSubview:self.content];
        
        //时间
        self.time=[[UILabel alloc] init];
        self.time.numberOfLines=1;
        self.time.font = [UIFont systemFontOfSize:14.0f];
        self.time.textColor=[UIColor colorWithHexString:@"#7F92A1"];
        [self.contentView addSubview:self.time];
        //录制时间
        self.recorderTime=[[UILabel alloc] init];
        self.recorderTime.numberOfLines=1;
        self.recorderTime.font = [UIFont systemFontOfSize:16.0f];
        self.recorderTime.textColor=[UIColor whiteColor];
        [self.backImg addSubview:self.recorderTime];
    }
    return self;
}

- (void)refreshCell:(KKChatM *)chatInfo index:(long)index
{

    if(index==0){
        if([self.adviceFirst.asktype isEqualToString:@"0"]){
            // 首先计算文本宽度和高度
            CGRect rec = [self.adviceFirst.question boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
            // 气泡
            UIImage *image = nil;
            // 模拟左边
            if (self.flag==1)
            {
                self.backImg.frame = CGRectMake(10, 10, rec.size.width + 20, rec.size.height+10);
                self.content.textColor=[UIColor whiteColor];
                image = [UIImage imageNamed:@"bluemini_chat"];
                self.time.frame=CGRectMake(10, rec.size.height+20, 200, 16);
            }
            else // 模拟右边
            {
                self.backImg.frame = CGRectMake(screenWidth - 10 - rec.size.width - 20, 10, rec.size.width + 20, rec.size.height + 10);
                self.content.textColor=[UIColor blackColor];
                image = [UIImage imageNamed:@"greenmini-chat"];
                self.time.frame=CGRectMake(screenWidth - 10-140, rec.size.height+20, 200, 16);
            }
            // 拉伸图片 参数1 代表从左侧到指定像素禁止拉伸，该像素之后拉伸，参数2 代表从上面到指定像素禁止拉伸，该像素以下就拉伸
            image = [image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:image.size.height/2+15];
            self.backImg.image = image;
            // 文本内容的frame
            self.content.frame = CGRectMake(self.flag==1 ? 13 : 5, 5, rec.size.width, rec.size.height);
            self.content.text = self.adviceFirst.question;
            //时间
            self.time.text=self.adviceFirst.sysdate;
        }else{
            // 气泡
            UIImage *image = nil;
            self.backImg.frame = CGRectMake(screenWidth - 10 - voiceWidth - 20, 10, voiceWidth + 20, voiceHeight + 10);
            image = [UIImage imageNamed:@"green-chat"];
            // 拉伸图片 参数1 代表从左侧到指定像素禁止拉伸，该像素之后拉伸，参数2 代表从上面到指定像素禁止拉伸，该像素以下就拉伸
            image = [image stretchableImageWithLeftCapWidth:70 topCapHeight:image.size.height/2+15];
            self.backImg.image = image;
            self.content.text=@"";
            //时间
            self.time.frame=CGRectMake(13, voiceHeight + 20, 200, 16);
        }
    }else{
        if([chatInfo.contenttype isEqualToString:@"0"]){
            // 首先计算文本宽度和高度
            CGRect rec = [chatInfo.content boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
            // 气泡
            UIImage *image = nil;
            // 模拟左边
            if (![chatInfo.uid isEqualToString:UID])
            {
                self.backImg.frame = CGRectMake(10, 10, rec.size.width + 20, rec.size.height+10);
                self.content.textColor=[UIColor whiteColor];
                image = [UIImage imageNamed:@"bluemini_chat"];
                self.time.frame=CGRectMake(10, rec.size.height+20, 200, 16);
                
            }
            else // 模拟右边
            {
                self.backImg.frame = CGRectMake(screenWidth - 10 - rec.size.width - 20, 10, rec.size.width + 20, rec.size.height + 10);
                self.content.textColor=[UIColor blackColor];
                image = [UIImage imageNamed:@"greenmini-chat"];
                self.time.frame=CGRectMake(screenWidth - 10-140, rec.size.height+20, 200, 16);
            }
            // 拉伸图片 参数1 代表从左侧到指定像素禁止拉伸，该像素之后拉伸，参数2 代表从上面到指定像素禁止拉伸，该像素以下就拉伸
            image = [image stretchableImageWithLeftCapWidth:image.size.width/2 topCapHeight:image.size.height/2+15];
            self.backImg.image = image;
            // 文本内容的frame
            self.content.frame = CGRectMake([chatInfo.uid isEqualToString:UID] ? 5 : 13, 5, rec.size.width, rec.size.height);
            self.content.text = chatInfo.content;
            //时间
            self.time.text=[KKAppUtils timeStampToDate1:chatInfo.systime];
            self.recorderTime.text=@"";
        }else{
            // 气泡
            UIImage *image = nil;
            // 模拟左边
            if (![chatInfo.uid isEqualToString:UID])
            {
                self.backImg.frame = CGRectMake(10, 10, voiceWidth + 20, voiceHeight+10);
                image = [UIImage imageNamed:@"blue-chat"];
                self.time.frame=CGRectMake(10, voiceHeight+20, 200, 16);
                self.recorderTime.frame=CGRectMake(20, voiceHeight/2-3, 50, 16);
                self.recorderTime.textAlignment=NSTextAlignmentLeft;
                image = [image stretchableImageWithLeftCapWidth:20 topCapHeight:image.size.height/2+15];
            }
            else // 模拟右边
            {
                self.backImg.frame = CGRectMake(screenWidth - 10 - voiceWidth - 20, 10, voiceWidth + 20, voiceHeight + 10);
                image = [UIImage imageNamed:@"green-chat"];
                self.time.frame=CGRectMake(screenWidth - 10-140, voiceHeight+20, 200, 16);
                self.recorderTime.frame=CGRectMake(screenWidth - 10-120, voiceHeight/2-3, 50, 16);
                self.recorderTime.textAlignment=NSTextAlignmentRight;
                image = [image stretchableImageWithLeftCapWidth:70 topCapHeight:image.size.height/2+15];
            }
            // 拉伸图片 参数1 代表从左侧到指定像素禁止拉伸，该像素之后拉伸，参数2 代表从上面到指定像素禁止拉伸，该像素以下就拉伸
            self.backImg.image = image;
            self.content.text=@"";
            //时间
            self.time.text=[KKAppUtils timeStampToDate1:chatInfo.systime];
            self.recorderTime.text=chatInfo.times;
        }
    }
}
@end
